package com.jckl.foodathon.services.store;

import java.util.List;

import org.restlet.engine.util.Base64;
import org.restlet.ext.jackson.JacksonRepresentation;
import org.restlet.representation.Representation;
import org.restlet.resource.ClientResource;

import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.jckl.foodathon.R;
import com.jckl.foodathon.common.constant.CommunicationConstants;
import com.jckl.foodathon.common.utils.DateUtil;
import com.jckl.foodathon.model.Blog;
import com.jckl.foodathon.model.BlogsListWidget;
import com.jckl.foodathon.model.Store;
import com.jckl.foodathon.widget.BlogsListAdapter;

public class GetBlogs {
	
	public void GetBlogsByStore(Activity activity, View view, Store store, List<BlogsListWidget> blogsResultWidget, BlogsListAdapter adapter){
		
		PerformBlogsQueryAsync async = new PerformBlogsQueryAsync(activity, view, store, blogsResultWidget, adapter);
		async.execute();
		
	}
	
	private class PerformBlogsQueryAsync extends AsyncTask<Void, Void, Boolean> {
		
		Activity activity;
		Store store;
		String blogsJson;
		ListView blogResult;
		List<BlogsListWidget> blogResultWidget;
		BlogsListAdapter adapter;
		View view;
		ProgressDialog progress;
		
		public PerformBlogsQueryAsync(Activity activity, View view, Store store, List<BlogsListWidget> blogsResultWidget, BlogsListAdapter adapter) {
			this.activity = activity;
			this.store = store;
			this.blogResult = (ListView) view.findViewById(R.id.blog_result_list);
			this.blogResultWidget = blogsResultWidget;
			this.adapter = adapter;
			this.view = view;
		}
		
		@Override
		protected void onPreExecute() {
			progress = new ProgressDialog(activity);
			progress.setTitle(activity.getResources().getString(R.string.loading_title));
			progress.setMessage(activity.getResources().getString(R.string.loading_message));
			progress.show();
		}
		
		@Override
		protected Boolean doInBackground(Void... nothing) {
		
			boolean blogQuerySuccess = false;
			
			try{
				ClientResource cr = new ClientResource(CommunicationConstants.SERVER_URI+CommunicationConstants.BLOG_STORE);
				JacksonRepresentation<Store> _store = new JacksonRepresentation<Store>(store);
				Representation rep = cr.put(_store);
				blogsJson = rep.getText();
				blogQuerySuccess = true;
				
			} catch (Exception ex){
				Log.e(CommunicationConstants.APP_TAG, "Unable to get blogs based on store");
				ex.printStackTrace();
			}
			
			return blogQuerySuccess;
		}
		
		@Override
		protected void onProgressUpdate(Void... nothing){
			
	    }
		
		@Override
		protected void onPostExecute(Boolean successExecution) {
			progress.dismiss();
			
			if(successExecution){
				Gson gson = new Gson();
				Blog[] blogs = gson.fromJson(blogsJson, Blog[].class);
				for(int i=0;i<blogs.length;i++){
					Blog blog = blogs[i];
					BlogsListWidget blogResult = new BlogsListWidget();
					blogResult.setBloggerName(blog.getBlogger().getName());
					blogResult.setBlogTitle(blog.getTitle());
					blogResult.setPostDate(DateUtil.getDateInFormat(blog.getPostDate(), activity.getResources().getString(R.string.date_format)));
					byte[] bloggerImageByteArray = Base64.decode(blog.getBlogger().getEncodedImage());
					blogResult.setBloggerImage(BitmapFactory.decodeByteArray(bloggerImageByteArray, 0, bloggerImageByteArray.length));
					blogResultWidget.add(blogResult);
				}
				adapter = new BlogsListAdapter(activity.getApplicationContext(), blogResultWidget);
				blogResult.setAdapter(adapter);
			}else{
				Toast.makeText(activity.getApplicationContext(), "Blog Query Failed", Toast.LENGTH_SHORT).show();
			}
			
		}
	}

}
